﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EZMemoLib
{
    public class MyBookList
    {
        public MyBookList()
        { 

        }

        static MyBookList _MyBookList = null;
        public static MyBookList Instance()
        {
            if (_MyBookList == null)
            {
                _MyBookList = new MyBookList();
            }

            return _MyBookList;

        }

        EZMemoEntities context = new EZMemoEntities();
        public List<MyBookModel> GetMyBookList()
        {
            string loginID = Context.Login.LoginID;  
            List<MyBookModel> result = null;

            if (!string.IsNullOrEmpty(loginID))
            {
                var list = from b in context.MyBook
                           join c in context.MemoCategory
                           on b.CategoryID equals c.CategoryID
                           select new MyBookModel { LoginID = b.LoginID, CategoryID = c.CategoryID, CategoryName = c.CategoryName, CategoryDesc = c.CategoryDesc, ItemTotal = (c.ItemTotal == null ? 0 : c.ItemTotal.Value), ItemDone = b.ItemDone, ItemDrill = b.ItemDrill, ItemPlan = b.PlanTotal, Status = b.Status };

                list = list.Where(c => c.LoginID == loginID);

                result = list.ToList();
            }
            else
            {
                //show all category
                var list = from c in context.MemoCategory.Where ( c => c.CategoryLevel == (long)CategoryLevel.Book && c.CategoryStatus == (int)Status.Activited  )
                           select new MyBookModel { LoginID = "",  CategoryID = c.CategoryID, CategoryName = c.CategoryName, CategoryDesc = c.CategoryDesc, ItemTotal = (c.ItemTotal == null ? 0 : c.ItemTotal.Value), ItemDone = 0, ItemDrill = 0, ItemPlan = 0, Status = 0 };

                result = list.ToList();

            }
            return result;
        }

        //public List<MyBookModel> GetBookList(string loginID)
        //{
        //    var list = from l in context.MemoCategory 
        //                j join
        //               on b.CategoryID equals c.CategoryID
        //               select new MyBookModel { LoginID = b.LoginID, CategoryName = c.CategoryName, CategoryDesc = c.CategoryDesc, ItemTotal = (c.ItemTotal == null ? 0 : c.ItemTotal.Value), ItemDone = b.ItemDone, ItemDrill = b.ItemDrill, ItemPlan = b.PlanTotal, Status = b.Status };

        //    //var list = context.MemoCategory.Where(c => c.CategoryLevel == (int)CategoryLevel.Book  );

        //    if (!string.IsNullOrEmpty(loginID))
        //    {
        //        list = list.Where(c => c.LoginID == loginID);
        //    }
        //    return list.ToList();
        //}


        public void SaveMyBook(MemoCategory category, int status)
        {
            if (category == null)
                return;

            //int itemID = _Item
            string loginID = Context.Login.LoginID;  
            MyBook mybook = context.MyBook.Where(c => c.CategoryID == category.CategoryID && c.LoginID == loginID ).FirstOrDefault();
            if (mybook == null)
            {
                mybook = new MyBook
                {
                    MyBookID = Guid.NewGuid (),
                    CategoryID = category.CategoryID,
                    LoginID = loginID,
                    StartDate = DateTime.Now,
                    ItemDone = 0,
                    ItemDrill = 0,
                    PlanTotal = 0,
                    Status = status,
                    PlanUnit = 0,
                    TodayFlow = "",
                    TodayStatus = 0,
                    OrderNumber = 1,
                    LastDate = DateTime.Now
                };

                context.AddToMyBook(mybook);
            }
            else
            {
                mybook.Status = status;
            }
            int rc = context.SaveChanges();

        }
    }
}
